package com.lenovo.manufacture;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.lenovo.manufacture.model.MaterialInfo;

import java.util.ArrayList;
import java.util.List;

public class ShoppingActivity extends AppCompatActivity {

    private TextView tv_six_total_price;
    private ListView lv_eight_item_shop;
    private List<MaterialInfo.DataBean> list = new ArrayList<>();
    private ArrayAdapter<MaterialInfo.DataBean> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_eight_shop);
        initView();
        getData();
    }

    private void getData() {
        Intent intent = getIntent();
        String stringExtra = intent.getStringExtra(Race8Activity.KEY);
        MaterialInfo materialInfo = new Gson().fromJson(stringExtra, MaterialInfo.class);
        list.addAll(materialInfo.getData());
        adapter.notifyDataSetChanged();
        int sum = 0;
        for (MaterialInfo.DataBean b : list) {
            sum += b.getPrice() * b.getNum();
        }
        tv_six_total_price.setText(Race8Activity.DECIMAL_FORMAT.format(sum));
    }

    private void initView() {
        tv_six_total_price = (TextView) findViewById(R.id.tv_six_total_price);
        lv_eight_item_shop = (ListView) findViewById(R.id.lv_eight_item_shop);
        adapter = new ArrayAdapter<MaterialInfo.DataBean>(this, R.layout.item_eight_lv, list) {
            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_eight_lv, parent, false);
                Race8Activity.ViewHolder viewHolder = new Race8Activity.ViewHolder(convertView);
                MaterialInfo.DataBean item = getItem(position);
                viewHolder.tv_item_eight_lv_id.setText("" + item.getMaterialId());
                viewHolder.tv_item_eight_lv_number.setText(Race8Activity.DECIMAL_FORMAT.format(item.getNum()));
                viewHolder.tv_item_eight_lv_price.setText(Race8Activity.DECIMAL_FORMAT.format(item.getPrice()));
                viewHolder.tv_item_eight_lv_shop.setText(item.getSupplyName());
                viewHolder.tv_item_eight_lv_shopname.setText(item.getContent());
                return convertView;
            }
        };
        lv_eight_item_shop.setAdapter(adapter);
    }
}
